/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unipampa.view;

import br.unipampa.dados.DBConection;
import br.unipampa.dados.DBTelefone;
import br.unipampa.utilitarios.ExecutaSom;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Gabriel Moro
 */
public class PaneTelefones extends javax.swing.JPanel implements EfeitosSonoros {

    //OPERACOES
    private CadastroDeTelefone cadastroTel;
    private EdicaoDeTelefone edicaoTel;
    //MODELOS DAS TABELAS
    private DefaultTableModel modeloTableTelefones;
    private TableColumn modeloColunaI;
    private TableColumn modeloColunaII;
    //BANCOS
    private DBConection conexao;
    private DBTelefone dbTelefones;

    /**
     * Creates new form PaneTelefones
     */
    public PaneTelefones() {
        initComponents();

        this.modeloTableTelefones = (DefaultTableModel) this.jTableFones.getModel();
        this.jTableFones.getTableHeader().setReorderingAllowed(false);
        jScrollPaneTableTelefones.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnListarContatos = new javax.swing.JButton();
        lblNomeContato = new javax.swing.JLabel();
        txtContato = new javax.swing.JTextField();
        jbtnPesquisarTelefon = new javax.swing.JButton();
        jScrollPaneTableTelefones = new javax.swing.JScrollPane();
        jTableFones = new javax.swing.JTable();
        jPanelAcoes3 = new javax.swing.JPanel();
        btnNovoTel = new javax.swing.JButton();
        btnEditarTel = new javax.swing.JButton();
        btnExcluirTel = new javax.swing.JButton();

        setBackground(new java.awt.Color(204, 204, 255));

        btnListarContatos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/updated.png.png"))); // NOI18N
        btnListarContatos.setToolTipText("Listar Telefones");
        btnListarContatos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListarContatosActionPerformed(evt);
            }
        });
        btnListarContatos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnListarContatosKeyPressed(evt);
            }
        });

        lblNomeContato.setText("Nome do Contato:");

        jbtnPesquisarTelefon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/pesquisar.png"))); // NOI18N
        jbtnPesquisarTelefon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPesquisarTelefonActionPerformed(evt);
            }
        });
        jbtnPesquisarTelefon.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbtnPesquisarTelefonKeyPressed(evt);
            }
        });

        jTableFones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nome", "Telefone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableFones.getTableHeader().setResizingAllowed(false);
        jTableFones.getTableHeader().setReorderingAllowed(false);
        jTableFones.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableFonesKeyPressed(evt);
            }
        });
        jScrollPaneTableTelefones.setViewportView(jTableFones);

        jPanelAcoes3.setBackground(new java.awt.Color(204, 204, 204));
        jPanelAcoes3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Ações", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP, null, new java.awt.Color(0, 0, 0)));

        btnNovoTel.setBackground(new java.awt.Color(153, 180, 209));
        btnNovoTel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/novo.png"))); // NOI18N
        btnNovoTel.setToolTipText("Cadastrar telefone");
        btnNovoTel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoTelActionPerformed(evt);
            }
        });

        btnEditarTel.setBackground(new java.awt.Color(153, 180, 209));
        btnEditarTel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/editar.png"))); // NOI18N
        btnEditarTel.setToolTipText("Editar telefone");
        btnEditarTel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarTelActionPerformed(evt);
            }
        });

        btnExcluirTel.setBackground(new java.awt.Color(153, 180, 209));
        btnExcluirTel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/lixeira.png"))); // NOI18N
        btnExcluirTel.setToolTipText("Excluir telefone");
        btnExcluirTel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirTelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelAcoes3Layout = new javax.swing.GroupLayout(jPanelAcoes3);
        jPanelAcoes3.setLayout(jPanelAcoes3Layout);
        jPanelAcoes3Layout.setHorizontalGroup(
            jPanelAcoes3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAcoes3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelAcoes3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNovoTel, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditarTel, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnExcluirTel, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelAcoes3Layout.setVerticalGroup(
            jPanelAcoes3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAcoes3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNovoTel, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditarTel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnExcluirTel)
                .addGap(6, 6, 6))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelAcoes3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnListarContatos, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneTableTelefones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblNomeContato)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtContato, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(61, 61, 61)
                        .addComponent(jbtnPesquisarTelefon, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtnPesquisarTelefon)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(lblNomeContato)
                                    .addComponent(txtContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnListarContatos, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanelAcoes3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPaneTableTelefones, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnListarContatosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListarContatosActionPerformed
        efeitoSonoro();
        evtListarContatos();
    }//GEN-LAST:event_btnListarContatosActionPerformed

    private void jbtnPesquisarTelefonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPesquisarTelefonActionPerformed
        efeitoSonoro();
        evtPesquisar();
    }//GEN-LAST:event_jbtnPesquisarTelefonActionPerformed

    private void btnNovoTelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoTelActionPerformed
        efeitoSonoro();
        this.cadastroTel = new CadastroDeTelefone();
        this.cadastroTel.setVisible(true);
    }//GEN-LAST:event_btnNovoTelActionPerformed

    private void btnEditarTelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarTelActionPerformed
        efeitoSonoro();
        editarTelefEvent();
    }//GEN-LAST:event_btnEditarTelActionPerformed

    private void btnExcluirTelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirTelActionPerformed
        efeitoSonoro();
        excluirTelefEvent();
    }//GEN-LAST:event_btnExcluirTelActionPerformed

    private void btnListarContatosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnListarContatosKeyPressed
        if (evt.getKeyCode() == evt.VK_F5) {
            efeitoSonoro();
            evtListarContatos();
        }
    }//GEN-LAST:event_btnListarContatosKeyPressed

    private void jbtnPesquisarTelefonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbtnPesquisarTelefonKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            efeitoSonoro();
            evtPesquisar();
        }
    }//GEN-LAST:event_jbtnPesquisarTelefonKeyPressed

    private void jTableFonesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableFonesKeyPressed
        if (evt.getKeyCode() == evt.VK_E) {
            efeitoSonoro();
            editarTelefEvent();
        }
        if (evt.getKeyCode() == evt.VK_DELETE) {
            efeitoSonoro();
            excluirTelefEvent();
        }
        if (evt.getKeyCode() == evt.VK_F5) {
            efeitoSonoro();
            evtListarContatos();
        }
    }//GEN-LAST:event_jTableFonesKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditarTel;
    private javax.swing.JButton btnExcluirTel;
    private javax.swing.JButton btnListarContatos;
    private javax.swing.JButton btnNovoTel;
    private javax.swing.JPanel jPanelAcoes3;
    private javax.swing.JScrollPane jScrollPaneTableTelefones;
    private javax.swing.JTable jTableFones;
    private javax.swing.JButton jbtnPesquisarTelefon;
    private javax.swing.JLabel lblNomeContato;
    private javax.swing.JTextField txtContato;
    // End of variables declaration//GEN-END:variables

    private void preencheTabelaFones(String nome) {
        this.modeloTableTelefones.setNumRows(0);//limpa toda a tabela
        try {
            this.conexao = new DBConection();
            this.dbTelefones = new DBTelefone(this.conexao.getCon());
            ArrayList<Object> valores = this.dbTelefones.select(nome);
            for (int x = 0; x < valores.size(); x = x + 2) {
                this.modeloTableTelefones.addRow(new Object[]{
                    valores.get(x),
                    valores.get(x + 1)});
            }
        } catch (SQLException erroSql) {
            ApoioDeInterface.mensagemErroBanco(erroSql.getMessage());
        } finally {
            this.conexao.encerrarConexao();
            this.dbTelefones.closedConnection();
            redimensionarColumnsComp();
        }
    }

    private void atualizaTabelaFones() {
        this.modeloTableTelefones.setNumRows(0);//limpa toda a tabela
        try {
            this.conexao = new DBConection();
            this.dbTelefones = new DBTelefone(this.conexao.getCon());
            ArrayList<Object> valores = this.dbTelefones.selected();
            for (int x = 0; x < valores.size(); x = x + 2) {
                this.modeloTableTelefones.addRow(new Object[]{
                    valores.get(x),
                    valores.get(x + 1)});
            }
        } catch (SQLException erroSql) {
            ApoioDeInterface.mensagemErroBanco(erroSql.getMessage());
        } finally {
            this.conexao.encerrarConexao();
            this.dbTelefones.closedConnection();
            redimensionarColumnsComp();
        }
    }

    private void redimensionarColumnsComp() {
        this.jTableFones.setAutoResizeMode(this.jTableFones.AUTO_RESIZE_OFF);
        this.modeloColunaI = this.jTableFones.getColumnModel().getColumn(0);
        this.modeloColunaI.setPreferredWidth(320);
        this.modeloColunaII = this.jTableFones.getColumnModel().getColumn(1);
        this.modeloColunaII.setPreferredWidth(110);
    }

    private void evtListarContatos() {
        atualizaTabelaFones();
        jScrollPaneTableTelefones.setVisible(true);
        jScrollPaneTableTelefones.revalidate();
        revalidate();
    }

    private void evtPesquisar() {
        String contat = this.txtContato.getText();
        preencheTabelaFones(contat);
        this.jScrollPaneTableTelefones.setVisible(true);
    }

    private void excluirTelefEvent() {
        try {
            if (ApoioDeInterface.mensagemRealizarTarefa()) {

                int numberLine = this.jTableFones.getSelectedRow();
                Object x = this.modeloTableTelefones.getValueAt(numberLine, 0);
                String valor = String.valueOf(x);

                this.conexao = new DBConection();
                this.dbTelefones = new DBTelefone(this.conexao.getCon());
                if (this.dbTelefones.delete(valor) == 1) {
                    ApoioDeInterface.mensagemDeDelete();

                    //PREPARA TABLE
                    atualizaTabelaFones();
                    jScrollPaneTableTelefones.setVisible(true);
                    revalidate();
                }
            }
        } catch (SQLException erroSQL) {
            ApoioDeInterface.mensagemErroBanco(erroSQL.getMessage());
        } catch (ArrayIndexOutOfBoundsException erro) {
            ApoioDeInterface.mensagemDeNaoSelecao();
        }
    }

    private void editarTelefEvent() {
        try {
            if (ApoioDeInterface.mensagemRealizarTarefa()) {
                int numberLine = this.jTableFones.getSelectedRow();
                Object x = this.modeloTableTelefones.getValueAt(numberLine, 0);
                String nome = String.valueOf(x);
                this.edicaoTel = new EdicaoDeTelefone(nome);
                this.edicaoTel.setVisible(true);
            }
        } catch (ArrayIndexOutOfBoundsException erro) {
            ApoioDeInterface.mensagemDeNaoSelecao();
        }
    }

    @Override
    public void efeitoSonoro() {
        ExecutaSom exeSound = new ExecutaSom();
        exeSound.executar("/br/unipampa/sounds/click2.wav");
    }
}
